package com.mayisoftware.mayioa.business.flow.impl.serviceImpl;

import com.mayisoftware.mayioa.business.flow.api.domain.BaseFlowInfo;
import com.mayisoftware.mayioa.business.flow.api.service.IFlowBaseService;
import com.mayisoftware.mayioa.common.impl.utils.bean.ReflectionUtil;
import com.mayisoftware.mayioa.platform.impl.shiro.realm.UserRealm;
import com.mayisoftware.mayioa.platform.impl.util.SpringUtils;
import org.apache.commons.collections4.MapUtils;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;

/**
 * 通用的计划提交系统审核任务处理
 */
@Component
public class SignCheckSystemListener implements TaskListener {

    /**
     * 日志类
     */
    private static final Logger log = LoggerFactory.getLogger(SignCheckSystemListener.class);

    /**
     * 监听创建任务，设置系统处理人
     */
    @Override
    public void notify(DelegateTask delegateTask) {
        log.warn("设置通用检查服务用户 CommonCheckSystem");

        delegateTask.setAssignee("CommonCheckSystem");

        log.warn("设置通用检查服务用户 "+delegateTask.getId() +" "+delegateTask.getName());

        Field[] fields = ReflectionUtil.getAllDeclareFields(BaseFlowInfo.class);
        delegateTask.setVariable("flowName",delegateTask.getName());
        delegateTask.setVariable("outcome","通过");
        delegateTask.setVariable("remark","参与报名完成");

        for (Field field:fields){
            Object tmpValue =delegateTask.getVariable(field.getName());
            if (tmpValue == null && field.getName().equalsIgnoreCase("ApprovalUserId")){
                tmpValue = "system";
            }

            if (tmpValue == null && field.getName().equalsIgnoreCase("ApprovalUserName")){
                tmpValue = "system";
            }

            delegateTask.setVariable(delegateTask.getTaskDefinitionKey()+"|"+field.getName(),tmpValue);
        }

        SpringUtils.getBean(TaskService.class).complete(delegateTask.getId());
    }
}
